// 依赖实例
export default class Watcher {
    constructor (vm, expOrFn, cb) {
        this.vm = vm;
        this.cb = cb;
        this.getter = parsePath(expOrFn);
        this.value = this.get();
    }

    get() {
        // 通过window.target = this把实例自身赋给了全局的一个唯一对象
        window.target = this;
        const vm = this.vm;
        let value = this.getter.call(vm, vm);
        // 释放依赖
        window.target = void(0);
        return value;
    }

    update() {
        const oldValue = this.value;
        thiss.value = this.get();
        this.cb.call(this.vm, this.value, oldValue);
    }
}

/**
 * Parse simple path.
 * 把一个形如'data.a.b.c'的字符串路径所表示的值，从真实的data对象中取出来
 * 例如：
 * data = {a:{b:{c:2}}}
 * parsePath('a.b.c')(data)  // 2
 */
const bailRE = /[^\w.$]/;
export function parsePath (path) {
    if (bailRE.test(path)) {
        return ;
    }
    const segments = path.split(".");
    return function (obj) {
        for (let i = 0; i < segments.length; i++) {
            if (!obj) return ;
            obj = obj[segments[i]];
        }
        return obj;
    }
}